#region Usings

using System;
using System.Collections.Generic;
using Castle.ActiveRecord;
using Karma.Framework.Core.Domain;
using Newtonsoft.Json;

#endregion

namespace Karma.Framework.Core.Metadata.Mapping
{
    [ActiveRecord("[Entity]")]
    public class Entity : DomainObject
    {
        [JoinedKey]
        public override Int64 Id { get; set; }

        [Property(NotNull = true, Unique = true)]
        public string Name { get; set; }

        [Property(NotNull = true)]
        public string Type { get; set; }

        [Property(NotNull = true)]
        public string Service { get; set; }

        [Property]
        public string NewTitle { get; set; }

        [Property]
        public string Title { get; set; }
        [Property("[Open]", NotNull = true)]
        public bool Open { get; set; }

        [Property("[New]", NotNull = true)]
        public bool New { get; set; }

        [Property("[Find]", NotNull = true)]
        public bool Find { get; set; }

        [Property("[Delete]", NotNull = true)]
        public bool Delete { get; set; }

        [Property("[Update]", NotNull = true)]
        public bool Update { get; set; }

        [Property("[Print]", NotNull = true)]
        public bool Print { get; set; }

        [Property("[Export]", NotNull = true)]
        public bool Export { get; set; }

        public Editor Editor
        {
            get { return (_Editors == null || _Editors.Count == 0 ? null : _Editors[0]); }
            set
            {
                if (_Editors == null)
                {
                    _Editors = new List<Editor> { value };
                }
                _Editors[0] = value;
            }
        }

        [JsonIgnore]
        [HasMany(ColumnKey = "[Entity]", Cascade = ManyRelationCascadeEnum.All,
            Lazy = false, Table = "[Editor]")]
        protected IList<Editor> _Editors { get; set; }


        [HasMany(typeof(View), Cascade = ManyRelationCascadeEnum.All,
            Lazy = false, ColumnKey = "[Entity]", Table = "[View]")]
        public IList<View> Views { get; set; }

        [HasMany(typeof (Query), Cascade = ManyRelationCascadeEnum.All, Lazy = false,
            ColumnKey = "[Entity]", Table = "[Query]")]
        public IList<Query> Queries { get; set; }

        [HasMany(typeof (Operation), Cascade = ManyRelationCascadeEnum.All,
            Lazy = false, ColumnKey = "[Entity]", Table = "[Operation]")]
        public IList<Operation> Operations { get; set; }

        [HasMany(typeof(Report), Cascade = ManyRelationCascadeEnum.All,
            Lazy = false, ColumnKey = "[Entity]", Table = "Report")]
        public IList<Report> Reports { get; set; }
    }
}